﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using AmkeniCrossLoginApplication.Data_Access;

namespace Amkeni_System.CO.Edit
{
    public partial class AddOutputIndicator : System.Web.UI.Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            /*if (User.IsInRole("IP"))
            {
                MasterPageFile = "~/MasterPages/Project.Master";
            }
            if (User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/Programme.Master";
            }
            if (User.IsInRole("Administrator"))
            {
                MasterPageFile = "~/MasterPages/CO.Master";
            }
            if (User.IsInRole("IP") && User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/ProgProject.Master";
            }*/
        }

        Amkeni_DatabaseEntities db = new Amkeni_DatabaseEntities();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            try
            {
                string ID = IndicatorIDTextBox.Text.Trim();

                var ProgIndicator = (from c in db.indicators
                                     where
                                         c.output_id == ID
                                     select c).FirstOrDefault();

                if (ProgIndicator == null)
                {
                    //==================================================+
                    //  Add a new Programme Output Indicator Record     |
                    //==================================================+
                    indicator newIndicator = new indicator();
                    newIndicator.indicator_type_id = 1;
                    newIndicator.output_indicator_id = ID;
                    newIndicator.output_id = OutputList.SelectedValue.Trim();
                    newIndicator.name = IndicatorTextBox.Text.Trim();
                    newIndicator.baseline = BaselineTextBox.Text.Trim();
                    newIndicator.target = TargetTextBox.Text.Trim();
                    newIndicator.data_source = DataSourceTextBox.Text.Trim();
                    newIndicator.method_of_collection = MethodOfCollectionTextBox.Text.Trim();
                    newIndicator.collection_tools = CollectionToolsTextBox.Text.Trim();
                    newIndicator.assumptions_and_risks = AssumptionsAndRisksTextBox.Text.Trim();
                    newIndicator.means_of_verification = MeansOfVerificationTextBox.Text.Trim();

                    db.indicators.Add(newIndicator);

                    error.Text = "SAVED";
                    error.Visible = true;

                    IndicatorIDTextBox.Text = null;
                    IndicatorTextBox.Text = null;
                    OutputList.SelectedValue = null;
                }
                else
                {
                    error.Text = "That Output Indicator ID already exists";
                    error.Visible = true;
                }
            }
            catch (Exception ex)
            {
                error.Text = "Database Error: " + ex.Message + "<br /> " + ex.StackTrace;
                error.Visible = true;
            }
            finally
            {
                db.SaveChanges();
            }
        }

        protected void CancelButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Amkeni_System/CO/Output_Indicators.aspx", true);
        }
    }
}